package utwente.hmi.touchmol.elements;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.Border;

import utwente.hmi.touchmol.*;

/**
 * User buttons are present on all the buttonpanels, and are the virtual representations of
 * the users. A button has a color, and belongs to the user standing on the mat with that color.
 * @author Jeroen Logtenberg
 */
public class UserButton extends JPanel {

	private static final long serialVersionUID = 1L;
	
	public final static int defaultWidth = 50;
	public final static int defaultHeight = 50;
	
	//These borders are used for the user buttons in a queue panel.
	public final static Border BORDER_TURN = BorderFactory.createLineBorder(Color.WHITE, 5);
	public final static Border BORDER_QUEUE = BorderFactory.createLineBorder(Color.DARK_GRAY, 5);
	public final static Border BORDER_QUEUE_1 = BorderFactory.createTitledBorder(BORDER_QUEUE, "1");
	public final static Border BORDER_QUEUE_2 = BorderFactory.createTitledBorder(BORDER_QUEUE, "2");
	public final static Border BORDER_QUEUE_3 = BorderFactory.createTitledBorder(BORDER_QUEUE, "3");
	public final static Border BORDER_QUEUE_4 = BorderFactory.createTitledBorder(BORDER_QUEUE, "4");
	
	private int userID;
	
	/**
	 * Makes a new user button for allocating turns
	 * @param xdim Width in pixels
	 * @param ydim Height in pixels
	 * @param c Color
	 * @param user The user this button represents, use TouchMolGUI.USER_[COLOR] constants.
	 */
	public UserButton(int xdim, int ydim, Color c, int user){
		
		userID = user;
		Dimension d = new Dimension(xdim, ydim);
		setSize(d);
		setMaximumSize(d);
		setPreferredSize(d);
		setMinimumSize(d);
		setBackground(c);
		setVisible(true);
	}
	
	/**
	 * 
	 * @return Returns TouchMolGUI.USER_[COLOR] for the user associated with this user button.
	 */
	public int getUserID(){
		return userID;
	}
	
	/**
	 * This method makes sure that no strange user buttons will appear.
	 * @param userID The ID of the user.
	 * @return The user button that belongs to the given user. If the userID is incorrect, null is returned.
	 */
	public static UserButton getButton(int userID){
		switch(userID){
		case TouchMolGUI.USER_RED: return getRedButton();
		case TouchMolGUI.USER_BLUE: return getBlueButton();
		case TouchMolGUI.USER_GREEN: return getGreenButton();
		case TouchMolGUI.USER_YELLOW: return getYellowButton();
		default: return null;
		}
	}
	
	/**
	 * Checks if a button is touched.
	 * @param location The location of touch.
	 * @return Returns if the button is touched.
	 */
	public boolean buttonTouched(Point location){
		Rectangle r = this.getBounds();
		Point p = this.getLocationOnScreen();
		r.setLocation(p);
		return r.contains(location);
	}
	
	/**
	 * Makes the red button.
	 * @return The red button.
	 */
	public static UserButton getRedButton(){
		return new UserButton(defaultWidth, defaultHeight, Color.RED, TouchMolGUI.USER_RED);
	}
	
	/**
	 * Makes the blue button.
	 * @return The blue button.
	 */
	public static UserButton getBlueButton(){
		return new UserButton(defaultWidth, defaultHeight, Color.BLUE, TouchMolGUI.USER_BLUE);
	}
	
	/**
	 * Makes the green button.
	 * @return The green button.
	 */
	public static UserButton getGreenButton(){
		return new UserButton(defaultWidth, defaultHeight, Color.GREEN, TouchMolGUI.USER_GREEN);
	}
	
	/**
	 * Makes the yellow button.
	 * @return The yellow button.
	 */
	public static UserButton getYellowButton(){
		return new UserButton(defaultWidth, defaultHeight, Color.YELLOW, TouchMolGUI.USER_YELLOW);
	}
	
}
